<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>分配权限</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend> &lt;&lt;<span id="roleName"></span>&gt;&gt; , 分配权限</legend>
        </fieldset>
        <div class="layui-btn-container">
            <button type="button" class="layui-btn layui-btn-md" lay-on="saveChecked">保存</button>
        </div>
        <div id="id-tree-demo"></div>
    </div>
</div>
<style>
    .layui-btn-container{
        position: fixed;
        z-index: 999;
    }
    #id-tree-demo{
        margin-left:80px;
    }
</style>
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="/js/common.js?v=5" charset="utf-8"></script>
<script>
Id = urlParam("id")
permissionIds = [];
detail = null;
layui.use(function () {
    var tree = layui.tree;
    var layer = layui.layer;
    var util = layui.util;
    var $ = layui.$;
    function getTreeData(data,pid){
        var children = []
        for(var k in data){
            var v = data[k];
            if (v.pid === pid){
                v.children = getTreeData(data,v.id);
                if (v.children.length===0 && permissionIds.indexOf(v.id) !==-1 ){ //叶子节点, 而且在权限内
                    v.checked = true;
                }
                children.push(v)
            }
        }
        return children;
    }

    function getTreeIds(data) {
        var ids = []
        for(var k in data){
            var v = data[k];
            ids.push(v.id)
            if (typeof v.children === 'object'){
                ids = ids.concat(getTreeIds(v.children));
            }
        }
        return ids;
    }

    $.get('/admin/role/'+Id,{},function (res) {
        detail = res.data;
        $('#roleName').text(detail.name);
        var allPermissions = res.data.allPermissions;
        permissionIds = res.data.permissionIds; //拥有的权限IDS
        for(var k in allPermissions){
            allPermissions[k].checked = false;
            allPermissions[k].spread = true;
            allPermissions[k].field = '';
        }
        var treeList = getTreeData(allPermissions,0);

        // 渲染
        tree.render({
            elem: '#id-tree-demo',
            data: treeList,
            showCheckbox: true,  // 是否显示复选框
            id: 'demo-id-1',
            isJump: true, // 是否允许点击节点时弹出新窗口跳转
            click: function(obj){
                var data = obj.data;  //获取当前点击的节点数据
//                layer.msg('状态：'+ obj.state + '<br>节点数据：' + JSON.stringify(data));
            }
        });
    })

    // tree.setChecked('demo-id-1', [12, 16]); // 勾选对应 id 值的节点

    // 按钮事件
    util.event('lay-on', {
        saveChecked: function(othis){
            var checkedData = tree.getChecked('demo-id-1'); // 获取选中节点的数据
            var ids = getTreeIds(checkedData);
            var postData = {_method:"put"}
            for(var i in ids){
                postData['permissionIds['+i+']'] = ids[i];
            }
            $.post('/admin/role/'+Id+'/givePermission',postData,function (res) {
                if(res.code ===0){
                    layer.msg('保存成功',{icon:6,time:500},function(){
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    })
                }
            })
        },
    });
})
</script>
</body>
</html>